package cn.hyxy.hadoop.writable;

import cn.hyxy.hadoop.writable.Demo13_AccessLog_zhenAi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;

public class Demo14_MyWritble_ZhenAiCount extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        if (args.length != 2) {
            System.out.println("usage : in out...");
            return -1;
        }
        // 7:声明Job
        Configuration config = getConf();
        FileSystem fs = FileSystem.get(config);
        Path path = new Path(args[1]);
        if (fs.exists(path)) {
            fs.delete(path, true);
        }
        // 8:Job
        Job job = Job.getInstance(config, "ZhenAiLogSort");
        job.setJarByClass(getClass());
        //
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Demo13_AccessLog_zhenAi.class);
        //
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);
        //
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, path);
        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        int code  = ToolRunner.run(new Demo14_MyWritble_ZhenAiCount(),args);
        System.exit(code);
    }

    
    public static class MyMapper extends Mapper<LongWritable, Text, Text, Demo13_AccessLog_zhenAi> {
    	private Text key2=new Text();
        @Override
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] str = value.toString().split(",+");
//            String[] str = value.toString().split("\\&#44");
            String id = str[0];
            String name = str[1];
            String address = str[2];
            String state = str[3];
            String height = str[4];
            String age = str[5];
            String sex=str[6];
            key2.set(str[0]);
//            System.out.println(key2.toString());
//            System.out.println(str[0]+"-----"+str[1]+"-----"+str[2]+"-----"+str[3]+"-----"+str[4]+"-----"+str[5]+"-----"+str[6]);
            Demo13_AccessLog_zhenAi log = new Demo13_AccessLog_zhenAi(id,name,address,state,height,age,Integer.parseInt(sex));
            context.write(key2,log);
        }
    }


    public static class MyReducer extends Reducer<Text, Demo13_AccessLog_zhenAi, Text, NullWritable> {
        private Text key4 = new Text();
        @Override
        protected void reduce(Text key, Iterable<Demo13_AccessLog_zhenAi> values,
        		Reducer<Text, Demo13_AccessLog_zhenAi, Text, NullWritable>.Context context)
        		throws IOException, InterruptedException {        	
        	Demo13_AccessLog_zhenAi value=values.iterator().next();
        	key4.set(value.getId()+"\t"+value.getName()+"\t"+value.getAddress()+"\t"+value.getState()+"\t"+value.getHeight()+"\t"+value.getAge()+"\t"+value.getSex());
            context.write(key4,NullWritable.get());
        }
    }
}
